<?php
namespace Admin\Controller;
use Think\Controller;
//use Think\PHPExcel;

class LoginController extends Controller {
    //ThinkPHP的构造函数
    public function _initialize() {
        header("Content-type:text/html;charset=utf-8");
    }
    //登录页
    public function login(){
        //如果已登录，则跳转到后台首页
        if(session('admin_id')||cookie('admin_id')){
            echo "<script>window.location.href='/index/'</script>";exit;
        }
        $data['title']='找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //登录提交
    public function login_post(){
        //提交
        if($_POST['admin_name']!=''&&$_POST['admin_passwd']!=''){
            $post['admin_name']=I('post.admin_name');
            $post['admin_passwd']=md5(I('post.admin_passwd'));
            //print_r($post);exit;
            $Model=D("Admin");
            $Model->post=$post;
            if(!$Model->check_admin()){
                echo "<script>alert('用户名/密码错误，请您重新输入');history.back(-1);</script>";exit;
            }
            $admin_id=$Model->where("admin_name='".$post['admin_name']."' and admin_passwd='".$post['admin_passwd']."'")->getField('admin_id');
            $admin_group_id=$Model->where("admin_id=".$admin_id)->getField('admin_group_id');
            //设置session
            session('admin_id',$admin_id);
            session('admin_group_id',$admin_group_id);
            //echo $_POST['remember'];exit;
            //设置cookie
            if($_POST['remember']==1){
                cookie('admin_id',$admin_id,7*24*60*60);  //设置cookie时长
                cookie('admin_group_id',$admin_group_id,7*24*60*60);  //设置cookie时长
            }
            //print_r(session());exit;
            echo "<script>window.location.href='/index'</script>";exit;
        }
    }
    //退出登录
    public function logout(){
        session('admin_id',null);
        session('admin_group_id',null);
        cookie('admin_id',null);
        cookie('admin_group_id',null);
        echo "<script>window.location.href='/login/login'</script>";exit;
    }

}